package com.example.orderservice.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.nacosdemo.dto.ChangeOrderDTO;
import com.example.nacosdemo.dto.ReduceDTO;
import com.example.nacosdemo.exception.ServiceException;
import com.example.orderservice.entity.Order;
import com.example.orderservice.feign.ProductClient;
import com.example.orderservice.mapper.OrderMapper;
import com.example.orderservice.producer.Producer;
import com.example.orderservice.service.OrderService;
import com.example.orderservice.util.SnowFlake;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final ProductClient productClient;

    private final Producer producer;


    @Override
    public void createOrder(ReduceDTO dto) {
        Order order = new Order();
        String orderNum = getOrderNum();
        order.setOrderNum(orderNum);
        order.setCreateTime(LocalDateTime.now());
        dto.setOrderNum(orderNum);
        this.saveOrUpdate(order);
        sendReduceMessage(dto);
    }

    private void sendReduceMessage(ReduceDTO dto) {
        producer.sendMessage("reduce", JSON.toJSONString(dto));
    }

    @Override
    public void changeOrder(ChangeOrderDTO dto) throws ServiceException {
        Order order = findByOrderNum(dto.getOrderNum());
        if (order == null){
            throw new ServiceException("订单不存在");
        }
        int status = getStatus(dto.getFlag());
        updateOrderStatus(dto.getOrderNum(), status);
    }

    private void updateOrderStatus(String orderNum, int status) {
        LambdaUpdateWrapper<Order> wrapper = Wrappers
                .<Order>lambdaUpdate()
                .eq(Order::getOrderNum, orderNum)
                .set(Order::getStatus, status);
        this.update(wrapper);
    }

    private int getStatus(Boolean flag) {
        int status = 2;
        if (flag){
            status = 1;
        }
        return status;
    }

    public Order findByOrderNum(String orderNum) {
        LambdaQueryWrapper<Order> wrapper = Wrappers
                .<Order>lambdaQuery()
                .eq(Order::getOrderNum, orderNum);
        Order one = this.getOne(wrapper);
        return one;
    }

    private Boolean reduceProduct() {
        Boolean flag = false;
        ReduceDTO reduceDTO = new ReduceDTO();
        reduceDTO.setProductCode("test1");
        flag = productClient.reduce(reduceDTO);
        return flag;
    }

    private static String getOrderNum() {
        SnowFlake flake = new SnowFlake(0,0);
        long l = flake.nextId();
        String orderNum = String.valueOf(l);
        return orderNum;
    }
}
